home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 40 / Amiga Format CD40 (1999-05-11)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-06].iso / -readerstuff- / iain_hamilton / density.c < prev    next >
C/C++ Source or Header  |  1999-03-27  |  6KB  |  269 lines

  1. /* Density Plotter
  2.    Written by Iain Hamilton
  3.    20th November 1998
  4.    compiled for 030 on VBCC
  5.    iain@slarti.demon.co.uk
  6.    http://www.slarti.demon.co.uk
  7.  
  8. */ 
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13.  
  14. void densplot();
  15. void clrscr();
  16.  
  17. main(void)
  18. {
  19.     int exitcode = 0;
  20.     int choice;
  21.  
  22. /* menu code starts here */
  23.  
  24. while (exitcode == 0)
  25.     {
  26.     clrscr();
  27.     printf("\n\t\tPerform a density plot for a given equation\n");
  28.     printf("\n\t\tPlease Choose an option from the menu..\n\n");
  29.  
  30.     printf("\t\t1......y^2+3x+x^3\n\n");
  31.     printf("\t\t2......sin x sin y\n\n");
  32.     printf("\t\t3......y/((x^2+y^2)^1.5)\n\n");
  33.     printf("\t\t4......x^3+y^3-2x\n\n");
  34.     printf("\t\t5......cos x + sin y(x^2+y)^1.5\n\n");
  35.     printf("\t\t6......Exit the Program\n\n");
  36.  
  37.     printf("\t\tPlease make your choice with the appropriate key : ");
  38.  
  39.     /*  Read the Keypress */
  40.  
  41.     choice = getch();
  42.     clrscr();
  43.  
  44.  
  45.     switch (choice)
  46.     {
  47.         case '1' : densplot(choice);
  48.                    break;
  49.         case '2' : densplot(choice);
  50.                    break;
  51.         case '3' : densplot(choice);
  52.                    break;
  53.         case '4' : densplot(choice);
  54.                    break;
  55.         case '5' : densplot(choice);
  56.                    break;
  57.         case '6' : exitcode = 1;
  58.                    break;
  59.         default  : clrscr();
  60.                    printf("\nChoice out of range, please try again...");
  61.                    break;
  62.     }
  63.     printf("\n\t\tPress a key to continue....\n");
  64.     getch();
  65.     }
  66. }
  67.  
  68. /* start of routine to calculate and plot density equations */
  69.  
  70. void densplot(choice)
  71. {
  72.     /* declare variables for use in procedure */
  73.  
  74.     float x;
  75.     float y;
  76.     int counter = 1;
  77.     float answer;
  78.     float highest;
  79.     float lowest;
  80.     float stepper;
  81.     float xlow;
  82.     float xhigh;
  83.     float ylow;
  84.     float yhigh;
  85.     float xstep;
  86.     float ystep;
  87.  
  88.     clrscr();
  89.  
  90.     /* check which equation is to be run and set the x&y ranges for the for loops */
  91.  
  92.     if (choice == 49)
  93.     {
  94.         xlow=-2.0;
  95.         xhigh=2.05;
  96.         ylow=-2.0;
  97.         yhigh=2.05;
  98.         xstep=0.1;
  99.         ystep=0.1;
  100.     }
  101.     else if (choice == 50)
  102.     {
  103.         xlow=0;
  104.         xhigh=3.14;
  105.         ylow=0;
  106.         yhigh=3.14;
  107.         xstep=0.1;
  108.         ystep=0.1;
  109.     }
  110.     else if (choice == 51)
  111.     {
  112.         xlow=1.0;
  113.         xhigh=3.05;
  114.         ylow=1.0;
  115.         yhigh=5.05;
  116.         xstep=0.1;
  117.         ystep=0.1;
  118.     }
  119.     else if (choice == 52)
  120.     {
  121.         xlow=0;
  122.         xhigh=4.05;
  123.         ylow=1.0;
  124.         yhigh=6.05;
  125.         xstep=0.1;
  126.         ystep=0.1;
  127.     }
  128.     else if (choice == 53)
  129.     {
  130.         xlow=-3;
  131.         xhigh=0.05;
  132.         ylow=-3;
  133.         yhigh=0.05;
  134.         xstep=0.1;
  135.         ystep=0.1;
  136.     }
  137.  
  138.     /* begin the for loops with the set x&y values */
  139.  
  140.     for (x=xlow; x<=xhigh; x=x+xstep)
  141.     {
  142.         for (y=ylow; y<=yhigh; y=y+ystep)
  143.         {
  144.  
  145.             /* calculate an equation depending on users selection */
  146.  
  147.             if (choice == 49)
  148.             {
  149.                 answer = (y*y)+(3*x)+(x*x*x);
  150.             }
  151.             else if (choice == 50)
  152.             {
  153.                 answer = sin(x)*sin(y);
  154.             }
  155.             else if (choice == 51)
  156.             {
  157.                 answer = y/pow(((x*x)+(y*y)),1.5);
  158.             }
  159.             else if (choice == 52)
  160.             {
  161.                 answer = (x*x*x)+(y*y*y)-(2*x);
  162.             }
  163.             else if (choice == 53)
  164.             {
  165.                 answer = cos(x)+ sin(y)*pow(((x*x)+y),1.5);
  166.             }
  167.  
  168.             /* get start values for lowest and highest */
  169.  
  170.             if (counter == 1)
  171.             {
  172.  
  173.                 lowest = answer;
  174.                 highest = answer;
  175.                 counter++;
  176.             }
  177.  
  178.             /* collect data for highest and lowest */
  179.  
  180.             if (answer < lowest)
  181.             {
  182.                 lowest = answer;
  183.             }
  184.             if (answer > highest)
  185.             {
  186.                 highest = answer;
  187.             }
  188.         }
  189.     }
  190.  
  191.     /* find the difference for highest & lowest and use this to create 7 sections */
  192.  
  193.     stepper = (highest - lowest)/7;
  194.  
  195.     /* begin the density plot */
  196.  
  197.     for (x=xlow; x<=xhigh; x=x+xstep)
  198.     {
  199.  
  200.         /* start a new line following x increment */
  201.         
  202.         printf("\n ");
  203.  
  204.         for (y=ylow; y<=yhigh; y=y+ystep)
  205.         {
  206.  
  207.             /* select equation to be run */       
  208.  
  209.             if (choice == 49)
  210.             {
  211.                 answer = (y*y)+(3*x)+(x*x*x);
  212.             }
  213.             else if (choice == 50)
  214.             {
  215.                 answer = sin(x)*sin(y);
  216.             }
  217.             else if (choice == 51)
  218.             {
  219.                 answer = y/pow(((x*x)+(y*y)),1.5);
  220.             }
  221.             else if (choice == 52)
  222.             {
  223.                 answer = (x*x*x)+(y*y*y)+(2*x);
  224.             }
  225.             else if (choice == 53)
  226.             {
  227.                 answer = cos(x)+sin(y)*pow(((x*x)+y),1.5);
  228.             }
  229.  
  230.             /* check which section the answer is in and print a character accordingly */
  231.  
  232.             if (answer <= (lowest+stepper))
  233.             {
  234.                 printf(".");
  235.             }
  236.             else if (answer > (lowest+stepper) && answer <= (lowest+(stepper*2)))
  237.             {
  238.                 printf(",");
  239.             }
  240.             else if (answer > (lowest+(stepper*2)) && answer <= (lowest+(stepper*3)))
  241.             {
  242.                 printf(";");
  243.             }
  244.             else if (answer > (lowest+(stepper*3)) && answer <= (lowest+(stepper*4)))
  245.             {
  246.                 printf("-");
  247.             }
  248.             else if (answer > (lowest+(stepper*4)) && answer <= (lowest+(stepper*5)))
  249.             {
  250.                 printf("+");
  251.             }
  252.             else if (answer > (lowest+(stepper*5)) && answer <= (lowest+(stepper*6)))
  253.             {
  254.                 printf("#");
  255.             }
  256.             else if (answer > (lowest+(stepper*6)) && answer <= highest)
  257.             {
  258.                 printf("*");
  259.             }
  260.         }
  261.     }
  262. }
  263.  
  264. void clrscr()
  265. {
  266.    /* this required for compile on amiga, can be removed for Turbo C/++ on the PC */
  267.    printf("\f");
  268. }
  269.